package net.bolue.web.mvc;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bolue.console.model.ExceptionModel;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.MvcResModel;
import net.bolue.sysmng.tblbean.MvcAction;
import net.bolue.sysmng.util.ReflectHelper;
import net.bolue.web.util.WriterUtil;

public class MainServlet extends HttpServlet{
	
	@Override
	public void init() throws ServletException {
		super.init();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.service(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.service(req, resp);
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String reqUri = request.getRequestURI();
		int i = reqUri.lastIndexOf("/");
		String str2 = reqUri.substring(i + 1, reqUri.length() - 3);
		MvcAction mvcAction = null;
		ActionResult actionResult = null;
		try {
			MvcResModel mvcResModel = new MvcResModel(request);
			mvcAction = mvcResModel.findMvcAction(str2);
			String actClass = mvcAction.getActClass();
			AbstractAction abstractAction = null;
			if ((actClass != null) && (actClass.trim().length() > 0)){
				Object localObject = ReflectHelper.classNewInstance(actClass);
				if(localObject!=null&&(localObject instanceof AbstractAction)){
					abstractAction = (AbstractAction)localObject;
				}
			}
			if(abstractAction!=null){
				actionResult = abstractAction.perform(request, response);
			}
		} catch (BolueException exception) {
			ExceptionModel exceptionModel = new ExceptionModel();
			actionResult = exceptionModel.ajaxResp(exception);
		}
		if(actionResult!=null){
			dealActionResult(actionResult,mvcAction,request,response);
		}
	}
	
	/**
	 * 处理action结果
	 * @param actionResult
	 * @param mvcAction
	 * @param request
	 * @param response
	 */
	private void dealActionResult(ActionResult actionResult, MvcAction mvcAction,HttpServletRequest request,HttpServletResponse response){
		if(mvcAction!=null){
			if(actionResult.getUrlPath()==null){
				actionResult.setUrlPath(mvcAction.getUrlPath());
			}
			if(actionResult.getIsRedirect()==null){
				actionResult.setIsRedirect(mvcAction.getIsRedirect());
			}
		}
		Integer returnType = actionResult.getReturnType();
		if(returnType==null){
			returnType = mvcAction.getActionType();
		}
		if(returnType==3){//ajax
//			Map returnData = actionResult.getReturnData();
//			WriterUtil.outPutMap(returnData, response);
			WriterUtil.outPutObject(actionResult, response);
		}else if(returnType==1){
			String urlPath = actionResult.getUrlPath();
			if(urlPath!=null&&!"".equals(urlPath)){
				try {
					request.getRequestDispatcher(urlPath).forward(request, response);
//					response.sendRedirect(urlPath);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	
}
